package net.contrend.material.dao;

import java.sql.Types;
import java.util.List;
import java.util.UUID;

import net.contrend.material.form.MaterialPurchaseSheetForm;
import net.contrend.material.mapper.MaterialPurchaseSheetMapper;
import net.contrend.system.dao.BaseDao;
import net.contrend.util.SqlUtils;
import net.contrend.util.StringUtils;

import org.springframework.jdbc.core.JdbcTemplate;

/**
 * <p>Copyright @ 2009 www.contrend.net</p>
 * <p>All right reserved. </p>
 * <p>项目名称			： 进销存管理系统 </p>
 * <p>JDK使用版本号		： jdk6.0 </P>
 * <p>描述				： 原料采购单 Dao </p>
 * @版本					： 1.0.0 
 * @author				： 谢庚才
 *
 * <p>修改历史 </p>
 * <p>修改时间            修改人员    修改内容 </p>
 * <p>Dec 11, 2009       谢庚才     新建 </p>
 */
public class MaterialPurchaseSheetDao extends BaseDao {
	private String table = "material_purchase_sheet";

	public MaterialPurchaseSheetDao() {
	}
	
	public MaterialPurchaseSheetDao(JdbcTemplate jt) {
        this.jt = jt;
    }
	
	public int insert(MaterialPurchaseSheetForm materialPurchaseSheet) {
		String columns = "mpsId, chargeMan, purDate, totalPrice, createDate, status";
        String sql = SqlUtils.getInsertSql(this.table, columns);
        System.out.println(String.format("\n%1$s\n", sql));
        
        if (StringUtils.isBlank(materialPurchaseSheet.getMpsId())) {
        	materialPurchaseSheet.setMpsId(UUID.randomUUID().toString());
        }
        
        return this.jt.update(sql,
                new Object[]{
        			materialPurchaseSheet.getMpsId(),
        			materialPurchaseSheet.getChargeMan(),
        			materialPurchaseSheet.getPurDate(),
        			materialPurchaseSheet.getTotalPrice(),
        			materialPurchaseSheet.getCreateDate(),
        			materialPurchaseSheet.getStatus()
                },
                new int[]{
        			Types.VARCHAR,
        			Types.VARCHAR,
        			Types.DATE,
        			Types.DOUBLE,
                    Types.DATE,
                    Types.VARCHAR
                });
	}
	
	public int update(MaterialPurchaseSheetForm materialPurchaseSheet) {
		String columns = "code, name, specification, unit, minQuantity, maxQuantiy, createDate, status";
        String sql = SqlUtils.getUpdateSql(this.table, columns, "mpsId = ?");
        System.out.println(String.format("\n%1$s\n", sql));
        return this.jt.update(sql,
        		new Object[]{
    			materialPurchaseSheet.getChargeMan(),
    			materialPurchaseSheet.getPurDate(),
    			materialPurchaseSheet.getTotalPrice(),
    			materialPurchaseSheet.getCreateDate(),
    			materialPurchaseSheet.getStatus()
            },
            new int[]{
    			Types.VARCHAR,
    			Types.DATE,
    			Types.DOUBLE,
                Types.DATE,
                Types.VARCHAR
            });
	}
	
	public int delete(String mpsId) {
        String sql = SqlUtils.getDeleteSql(this.table, "mpsId = ?");
        System.out.println(String.format("\n%1$s\n", sql));
        return this.jt.update(sql, new Object[]{mpsId}, new int[]{Types.VARCHAR});
    }
	
	@SuppressWarnings("unchecked")
	public MaterialPurchaseSheetForm queryById(String mpsId) {
        String sql = "select * from "+this.table+" where mpsId = ?";
        List<MaterialPurchaseSheetForm> materialPurchaseSheetList = this.jt.query(sql,
                new Object[]{mpsId}, new int[]{Types.VARCHAR}, new MaterialPurchaseSheetMapper());
        if (null != materialPurchaseSheetList && 0 < materialPurchaseSheetList.size()) {
            return materialPurchaseSheetList.get(0);
        }
        return null;
    }
	
}
